package com.gwt.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.gwt.client.event.SlideTransitionEvent;
import com.gwt.client.util.geo.Coordinates;
import com.gwt.client.util.geo.UserLocation;
import com.gwt.client.util.geo.UserLocation.UserLocationCallback;

public class Slide5 extends Composite implements Slide{

	private static final Binder binder = GWT.create(Binder.class);

	interface Binder extends UiBinder<Widget, Slide5> {/***/}

	private Slide.Presenter listener;
	private EventBus eventBus;
	
	@UiField FocusPanel slide;
	@UiField Button getLocButton;
	@UiField HTML userLocation;

	public Slide5(EventBus eventBus) {
		this.eventBus = eventBus;
		initWidget(binder.createAndBindUi(this));
	}
	
	@Override
	protected void onLoad() {
		slide.setFocus(true);
	}
	
	@UiHandler("getLocButton")
	public void getLoc(ClickEvent e){
		userLocation.setHTML("<br /><img src='../../../../ajax-loader.gif'/>");
		UserLocation.getLocation(new UserLocationCallback() {
			
			@Override
			public void onLocationFound(Coordinates coords) {
				String locationInfo = "<p><b>Latitude:</b> " + coords.getLatitude() + "</p>" +
						"<p><b>Longitude:</b> " + coords.getLongitude() + "</p>" +
						"<p><b>Altitude:</b> " + coords.getAltitude() + " meters</p>" +
						"<p><b>Accuracy:</b> &plusmn;" + coords.getAccuracy() + " meters</p>";
				userLocation.setHTML(locationInfo);
			}
		});
		
	}
	
	@UiHandler("slide")
	public void keyPress(KeyDownEvent e){
		eventBus.fireEvent(new SlideTransitionEvent(e));
	}

	public void setPresenter(Slide.Presenter listener){
		this.listener = listener;
	}

}